﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace System.Web.UI
{
    public class ZPage : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                MainLoad();
            }
            catch (Exception ex)
            {

                if (ex.InnerException != null)
                {
                    ZJScript.Alert(ex.InnerException.Message.Replace("'", "").Replace("\r\n", ""));
                }
                else
                {
                    ZJScript.Alert(ex.Message.Replace("'", "").Replace("\r\n", ""));
                }
                OnError(ex, "MainLoad");
                //Response.End();

                //ZJScript.Alert(ex.Message);
                //OnError(ex, "MainLoad");
                //Response.End();
            }
            base.OnLoad(e);
        }
        protected virtual void MainLoad()
        {
            this.OnInit();

            if (!IsPostBack)
            {
                this.OnLoading();
            }
            this.OnAllwaysLoading();
            if (!IsPostBack)
            {
                this.OnLoad();
            }
            this.OnAllwaysLoad();
            if (!IsPostBack)
            {
                this.OnLoaded();
            }
            this.OnAllwaysLoaded();
        }

        protected virtual void OnAllwaysLoad() { }
        protected virtual void OnAllwaysLoaded() { }
        protected virtual void OnAllwaysLoading() { }

        protected virtual void Reload()
        {
            this.OnInit();
            this.OnLoading();
            this.OnAllwaysLoading();
            this.OnLoad();
            this.OnAllwaysLoad();
            this.OnLoaded();
            this.OnAllwaysLoaded();
        }

        protected virtual void OnInit()
        {
            this.CheckRights();
            this.InitializeComponent();
        }

        protected virtual void InitializeComponent()
        {
            this.RegControls();
            this.RegControlsEvent();
            this.SetControlsProperty();
        }

        protected virtual void CheckRights() { }
        protected virtual void RegControls() { }
        protected virtual void RegControlsEvent() { }
        protected virtual void SetControlsProperty() { }
        protected virtual void OnLoading() { }

        protected virtual void OnLoad()
        {
            this.OnBinding();

            if (IsNeedBind)
            {
                this.OnBind();
                this.OnBinded();
            }
        }


        protected virtual bool IsNeedBind { get { return true; } }

        protected virtual void OnBinded() { }
        protected virtual void OnBind() { }
        protected virtual void OnBinding() { }

        protected virtual void OnLoaded() { }
        protected virtual void OnError() { }
        protected virtual void OnError(Exception ex, string method)
        {
            ZLog.SaveError(ex.GetBaseException(), method, ZRequest.IP, Request.Url.AbsoluteUri);
        }

        protected override void OnError(EventArgs e)
        {
            OnError();
            base.OnError(e);
        }      

        protected virtual void OnSubmited() { }
        protected virtual void OnSubmit() { }
        protected virtual void OnSubmiting() { }


        #region 常用调用方法

        protected virtual void ClearCache()
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
        }

        protected virtual void SetGridViewProperty(GridView gv)
        {
            gv.AutoGenerateColumns = false;
            gv.BorderWidth = 0;
            gv.CellPadding = 4;
            gv.EmptyDataText = "没有数据记录";
            gv.CssClass = "listTable";
            gv.AlternatingRowStyle.CssClass = "AlternatingRow";
            gv.AllowSorting = true;
            gv.Width = new Unit("100%");
        }

        #region 基本方法
        /// <summary>
        /// 退出浏览器
        /// </summary>
        protected virtual void Exit()
        {
            System.Web.Security.FormsAuthentication.SignOut();
            if (Session["account"] == null)
            {
                Session.RemoveAll();
            }
            else
            {
                Session.RemoveAll();
            }
            string Scriptstr = "<script language=javascript>window.close();</script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "bodyk", Scriptstr);
        }

        protected string GetQueryString(string requestName)
        {
            NameValueCollection utf8Requests;
            utf8Requests = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.UTF8);
            string strQueryString = "";
            if (utf8Requests[requestName] != null)
            {
                strQueryString = utf8Requests[requestName].ToString().Trim();
            }
            return strQueryString;
        }

        #endregion

        #endregion
    }
}
